package hxg.iptest;

import java.net.InetAddress;

/**
 * @author HeXiangGuang
 * @create 2021-06-29 20:54
 * 演示工netAddress类的使用
 * (1)创建本机IP地址对象
 * (2)创建局域网IP地址对象
 * (3)创建外网IP地址对象
 */
public class Demo1 {
    public static void main(String[] args) throws Exception{
        //1创建本机IP地址对象
        //1.1getLocalhost()方法
        InetAddress ia1 = InetAddress.getLocalHost();
        System.out.println("ip地址："+ia1.getHostAddress()+"主机名："+ia1.getHostName());
        //1.2getByName("ip地址")
        InetAddress ia2 = InetAddress.getByName("192.168.0.101");
        System.out.println("ip地址："+ia2.getHostAddress()+"主机名："+ia2.getHostName());
        //1.3getByName("127.0.0.1")
        InetAddress ia3 = InetAddress.getByName("127.0.0.1");
        System.out.println("ip地址："+ia3.getHostAddress()+"主机名："+ia3.getHostName());
        //1.2getByName("localhost")
        InetAddress ia4 = InetAddress.getByName("localhost");
        System.out.println("ip地址："+ia4.getHostAddress()+"主机名："+ia4.getHostName());


        //2创建局域网ip对象
//        InetAddress ia5 = InetAddress.getByName("192.168.0.102");
//        System.out.println("ip地址："+ia5.getHostAddress()+"主机名："+ia5.getHostName());
//        System.out.println("2秒钟是否可达"+ia5.isReachable(2000));


        //3创建外网ip地址对象
        InetAddress ia6 = InetAddress.getByName("www.baidu.com");
        System.out.println("ip地址："+ia6.getHostAddress()+"主机名："+ia6.getHostName());
        System.out.println("2秒钟是否可达"+ia6.isReachable(2000));
        System.out.println("------------------");
        //获取该域名下所有ip对象
        InetAddress[] allByName = InetAddress.getAllByName("www.baidu.com");
        for (InetAddress inetAddress : allByName) {
            System.out.println(inetAddress.getHostAddress());
        }

    }
}
